package com.rightsidetech.http.controller.agentAdmin.system;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.push.EnumMsgType;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.http.task.MassageTask;
import com.rightsidetech.system.dtos.request.BatteryCheckHandleReq;
import com.rightsidetech.system.dtos.request.BatteryCheckReq;
import com.rightsidetech.system.entity.SysAgentLoginInfo;
import com.rightsidetech.system.entity.SysBatteryCheck;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysBatteryCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Package: com.rightsidetech.http.controller.admin.system
 * @ClassName: SysBatteryCheckController
 * @Author: yinj
 * @CreateTime: 2022/01/05 16:18
 * @Description: 电池检测相关
 */
@RestController
@RequestMapping("/agentAdmin/battery_check")
@ShenyuSpringMvcClient(path = "/agentAdmin/battery_check/**")
@Api(tags = "agentAdmin:电池检测相关")
public class BatteryCheckController extends BaseController {

    @Resource
    private SysBatteryCheckService sysBatteryCheckService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private MassageTask massageTask;


    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @GetMapping("/page")
    @ApiOperation("电池检测列表")
    public ResponseData<PageInfo<SysBatteryCheck>> page(BatteryCheckReq request) throws Exception {

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        request.setAgentId(agentInfoId);

        return sysBatteryCheckService.page(request);
    }

    @Log
    @Token
    @Auth(bizType = Auth.AGENT)
    @PostMapping("/handle")
    @ApiOperation("处理")
    public ResponseData handle(@RequestBody BatteryCheckHandleReq request) throws Exception {

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        //获取登录账号代理商id
        SysAgentLoginInfo sysAgentLoginInfo = sysAgentInfoService.selectInfoById(Long.parseLong(getAttribute()));
        if (StringUtils.isEmpty(sysAgentLoginInfo)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        request.setAgentId(sysAgentLoginInfo.getAgentInfoId());
        request.setHandleName(sysAgentLoginInfo.getLoginName());

        ResponseData data = sysBatteryCheckService.handle(request);
        if (data.getCodeId() == 1){
            SysBatteryCheck batteryCheck = sysBatteryCheckService.selectByPrimaryKey(request.getId());
            if (!ObjectUtils.isEmpty(batteryCheck)){
                massageTask.storePush(batteryCheck.getStoreId(), EnumMsgType.BATTERY_CHECK.getName(),EnumMsgType.BATTERY_CHECK.getValue());
            }
        }
        return data;
    }


}
